package com.wwwarehouse.contract.contract.modify;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.bean.response.NotificationDetailsResponseBean;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.NewSettingRuleBean;
import com.wwwarehouse.contract.bean.ResourceItemBean;
import com.wwwarehouse.contract.bean.SettingRuleBean;
import com.wwwarehouse.contract.common.ContractCommon;
import com.wwwarehouse.contract.contract.MyBaseViewPagerFragment;
import com.wwwarehouse.contract.contract.ReleaseSettingRulesGoodsListFragment;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.core.ContractEventCode;
import com.wwwarehouse.contract.event.AddedGoodsDeleteEvent;
import com.wwwarehouse.contract.event.ContractEvent;
import com.wwwarehouse.contract.event.CountEvent;
import com.wwwarehouse.contract.event.DeleteEvent;
import com.wwwarehouse.contract.event.PublishSuccessEvent;
import com.wwwarehouse.contract.event.PublishingRulesEvent;
import com.wwwarehouse.contract.event.RefreshEvent;
import com.wwwarehouse.contract.event.RefreshPublishGoodsEvent;
import com.wwwarehouse.contract.event.UpdateShoppingCardEvent;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/10/19.
 *         <p/>
 *         本次新增商品  设置发布规则
 */

public class AddedSettingPublishingRulesFragment extends MyBaseViewPagerFragment {

	private String pbUkid;
	private String sort;
	private Bundle bundle;
	List<SettingRuleBean.DataBean.ListBean> mSelectList;
	private String mContractUkid;

	@Override
	public void setTitle() {
		if (peekFragment() instanceof AddedSettingPublishingRulesFragment) {
			if (mSelectList != null && mSelectList.size() > 0) {
				mActivity.setTitle(StringUtils.getResourceStr(mActivity, R.string.string_contract_modify_added_goods_title, mSelectList.size()));
			}
		}
	}

	@Override
	protected void loadDatas() {
		mConfirmBtn.setVisibility(View.VISIBLE);
		mConfirmBtn.setText(R.string.release);
		EventBus.getDefault().register(this);
		if (getArguments() == null || getArguments().getSerializable("select_list") == null || getArguments().getString(ContractConstant.KEY_MODIFY_PAYMENT_ITEM_CONTRACT_UKID) == null) {
			mLoadingView.setVisibility(View.VISIBLE);
			mLoadingView.showEmptyView(true);
			mActivity.setTitle(R.string.string_contract_modify_added_goods_title_none);
			mLoadingView.setEmptyListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					loadDatas();
				}
			});
			return;
		}
		if (getArguments() != null) {
			mContractUkid = getArguments().getString(ContractConstant.KEY_MODIFY_PAYMENT_ITEM_CONTRACT_UKID);
			sort = getArguments().getString("sort");
			mSelectList = (List<SettingRuleBean.DataBean.ListBean>) getArguments().getSerializable("select_list");
		}
		bundle = new Bundle();
		bundle.putString("pbUkid", pbUkid);
		bundle.putString("sort", sort);

		if (mSelectList == null || mSelectList.size() == 0) {
			mActivity.setTitle(R.string.string_contract_modify_added_goods_title_none);
			mLoadingView.setVisibility(View.VISIBLE);
			mLoadingView.showEmptyView(true);
			mLoadingView.setEmptyListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					loadDatas();
				}
			});
			return;
		}

		initData();



		mConfirmBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Map<String, Object> map = new HashMap<>();
				map.put("contractUkid", mContractUkid);
				List<NewSettingRuleBean.DataBean.ListBean> mNewList = new ArrayList<NewSettingRuleBean.DataBean.ListBean>();

				if (mSelectList != null && mSelectList.size() > 0) {
					for (int i = 0; i < mSelectList.size(); i++) {
						NewSettingRuleBean.DataBean.ListBean listBean = new NewSettingRuleBean.DataBean.ListBean();
						if (StringUtils.isNoneNullString(mSelectList.get(i).getBarCode())) {
							listBean.setBarCode(mSelectList.get(i).getBarCode());
						}
						if (StringUtils.isNoneNullString(mSelectList.get(i).getDefinedUkid())) {
							listBean.setDefinedUkid(mSelectList.get(i).getDefinedUkid());
						}
						if (StringUtils.isNoneNullString(mSelectList.get(i).getImg())) {
							listBean.setImg(mSelectList.get(i).getImg());
						}
						if (StringUtils.isNoneNullString(mSelectList.get(i).getName())) {
							listBean.setName(mSelectList.get(i).getName());
						}
						if (StringUtils.isNoneNullString(mSelectList.get(i).getPbPrice())) {
							listBean.setPbPrice(mSelectList.get(i).getPbPrice());
						}
						if (StringUtils.isNoneNullString(mSelectList.get(i).getPbQty())) {
							listBean.setPbQty(mSelectList.get(i).getPbQty());
						}
						if (StringUtils.isNoneNullString(mSelectList.get(i).getPbQtyUnit())) {
							listBean.setPbQtyUnit(mSelectList.get(i).getPbQtyUnit());
						}
						if (StringUtils.isNoneNullString(mSelectList.get(i).getPbQtyUnitUkid())) {
							listBean.setPbQtyUnitUkid(mSelectList.get(i).getPbQtyUnitUkid());
						}
						if (StringUtils.isNoneNullString(mSelectList.get(i).getPriceRuleType())) {
							listBean.setPriceRuleType(mSelectList.get(i).getPriceRuleType());
						}
						if (StringUtils.isNoneNullString(mSelectList.get(i).getPriceRuleValue())) {
							listBean.setPriceRuleValue(mSelectList.get(i).getPriceRuleValue());
						}
						if (StringUtils.isNoneNullString(mSelectList.get(i).getQty())) {
							listBean.setQty(mSelectList.get(i).getQty());
						}
						if (StringUtils.isNoneNullString(mSelectList.get(i).getQtyRuleType())) {
							listBean.setQtyRuleType(mSelectList.get(i).getQtyRuleType());
						}
						if (StringUtils.isNoneNullString(mSelectList.get(i).getQtyRuleValue())) {
							listBean.setQtyRuleValue(mSelectList.get(i).getQtyRuleValue());
						}
						if (StringUtils.isNoneNullString(mSelectList.get(i).getUnit())) {
							listBean.setUnit(mSelectList.get(i).getUnit());
						}
						mNewList.add(listBean);
					}
				}
				map.put("ddContractItemPublishes", mNewList);
				map.put("page", 1);
				httpPost(ContractConstant.METHOD_RESOURCE_MODIFY_SAVE_ADD_RESOURCE_ITEM, map, 0, true, "");


			}
		});
	}

	private void initData() {
		if (mSelectList.size() == 0) {
			mActivity.setTitle(R.string.string_contract_modify_added_goods_title_none);
			mLoadingView.setVisibility(View.VISIBLE);
			mLoadingView.showEmptyView(true);
			return;
		}
		mLoadingView.setVisibility(View.INVISIBLE);
		mActivity.setTitle(StringUtils.getResourceStr(mActivity, R.string.string_contract_modify_added_goods_title, mSelectList.size()));
		bundle.putSerializable("select_list", (Serializable) mSelectList);
		SetFragmentAndPage(mSelectList.size(), 5, AddedSettingRulesGoodsListFragment.class.getName(), bundle);
	}


	@Override
	public void onSuccess(CommonClass commonClass, int what) {
		if (what == 0) {
			if (TextUtils.equals("0", commonClass.getCode())) {
				popFragment();
				popFragment();
				popFragment();
				RefreshPublishGoodsEvent publishingRulesEvent = new RefreshPublishGoodsEvent("success");
				if (ContractCommon.getInstance().getModifyPublishGoodsData() != null && ContractCommon.getInstance().getModifyPublishGoodsData().size() > 0) {
					ContractCommon.getInstance().getModifyPublishGoodsData().clear();
				}
				EventBus.getDefault().post(publishingRulesEvent);
			} else if (commonClass.getCode().equals("703043")) {
				//只有code为这种情况时才用后端返回的msg
				if (StringUtils.isNullString(commonClass.getMsg())) {
					return;
				}
				toast(commonClass.getMsg());

			} else {
				toast(R.string.string_contract_resource_not_released);

			}
		}

	}

	public void onEventMainThread(Object event) {
		if (peekFragment() instanceof AddedSettingPublishingRulesFragment && event instanceof AddedGoodsDeleteEvent ) {
			SettingRuleBean.DataBean.ListBean item = ((AddedGoodsDeleteEvent) event).getMsg();
			if (mSelectList.contains(item)) {
				mSelectList.remove(item);
			}
			if (mSelectList.size() == 0) {
				mActivity.setTitle(R.string.string_contract_modify_added_goods_title_none);
				ShowEmptyAddedGoodsView();
				mConfirmBtn.setVisibility(View.GONE);
				return;
			}
			mLoadingView.setVisibility(View.INVISIBLE);
			mActivity.setTitle(StringUtils.getResourceStr(mActivity, R.string.string_contract_modify_added_goods_title, mSelectList.size()));
			bundle.putSerializable("select_list", (Serializable) mSelectList);
			SetFragmentAndPage(mSelectList.size(), 5, AddedSettingRulesGoodsListFragment.class.getName(), bundle);
		} else if (peekFragment() instanceof AddedSettingPublishingRulesFragment && event instanceof RefreshEvent) {
			if (mSelectList == null || mSelectList.size() == 0) {
				mLoadingView.setVisibility(View.VISIBLE);
				mLoadingView.showEmptyView(true);
				return;
			}
			mLoadingView.setVisibility(View.INVISIBLE);
			int position = ((RefreshEvent) event).getPosition();
			int page = ((RefreshEvent) event).getPage();
			int index = page * 5 + position;
			mSelectList.set(index, ((RefreshEvent) event).getBean());
			if (mSelectList.size() == 0) {
				mLoadingView.setVisibility(View.VISIBLE);
				mLoadingView.showEmptyView(true);
				return;
			}
			mLoadingView.setVisibility(View.INVISIBLE);
			mActivity.setTitle(StringUtils.getResourceStr(mActivity, R.string.string_contract_modify_added_goods_title, mSelectList.size()));
			bundle.putSerializable("select_list", (Serializable) mSelectList);
			SetFragmentAndPage(mSelectList.size(), 5, AddedSettingRulesGoodsListFragment.class.getName(), bundle);
		}

	}


	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}

	public void onEventMainThread(BackListenerEvent backListenerEvent) {
		if (peekFragment() instanceof AddedSettingPublishingRulesFragment && backListenerEvent.getMsg().equals("AddedSettingPublishingRulesFragment")) {
			popFragment();
			EventBus.getDefault().post(new ContractEvent<UpdateShoppingCardEvent>(ContractEventCode.CODE_REFRESH_MODIFY_PUBLISH_GOODS_SHOPPING_CARD , new UpdateShoppingCardEvent(mSelectList)));
		}
	}
}
